﻿using System;
using System.Collections.Generic;
using System.Data;
using DBManager.ProductUserDAL;
using System.Text;

namespace BO.ReWriteUrl
{
    public class BuildLink
    {
        #region Product
        public static string BuildLinkProduct(string ProductID)
        {
            string sFormat = "/{0}/{1}-p{2}.htm";
            string nameZone = "";
            string title = "";
            int zoneID = 0;
            DataTable dtProduct = ProductUserBO.ProductUBO.GetInfomationProductBO(ProductID);
            if (dtProduct != null)
            {
                DataRow dr = dtProduct.Rows[0];
                title = BO.Utils.BuildLinkUrlNice(dr[Const.tbProducts.Title].ToString());
                zoneID = int.Parse(dr[Const.tbProducts.ZoneId].ToString());
            }
            DataTable dtZone = ProductUserBO.ProductUBO.GetInfomationZoneBO(zoneID.ToString());
            if (dtZone != null)
            {
                DataRow dr = dtZone.Rows[0];
                nameZone = dr[Const.tbZoneProduct.url].ToString();
            }
            return string.Format(sFormat, nameZone, title, ProductID);

        }
        public static string BuildLinkZoneProduct(string zoneID)
        {
            string kq = "";
            DataTable dtZone = BO.ProductUserBO.ProductUBO.GetInfomationZoneBO(zoneID);
            if (dtZone != null && dtZone.Rows.Count > 0)
            {
                DataRow dr = dtZone.Rows[0];
                string url = dr[BO.Const.tbZoneProduct.url].ToString();
                string parentID = dr[BO.Const.tbZoneProduct.ParentId].ToString();
                //bat dau xet
                if (parentID == "0")
                {
                    //1 cap
                    kq = "/" + url + ".htm";
                }
                else
                {
                    //2 cap
                    DataTable dtZoneP = BO.ProductUserBO.ProductUBO.GetInfomationZoneBO(parentID);
                    if (dtZoneP != null && dtZoneP.Rows.Count > 0)
                    {
                        DataRow drP = dtZoneP.Rows[0];
                        string urlP = drP[BO.Const.tbZoneProduct.url].ToString();
                        kq = "/" + urlP + "/" + url + ".htm";
                    }
                }
            }
            //
            return kq;
        }
        #endregion
        #region News
        public static string BuildLinkNews(string NewsID)
        {
            string sFormat = "/{0}/{1}-{2}.htm";
            string nameZone = "";
            string title = "";
            int zoneID = 0;
            DataTable dtNews = News.NewsBO.GetDetailNewsBO(NewsID);
            if (dtNews != null)
            {
                DataRow dr = dtNews.Rows[0];
                title = BO.Utils.BuildLinkUrlNice(dr[Const.tbNews.Title].ToString());
                zoneID = int.Parse(dr[Const.tbNews.ZoneId].ToString());
            }
            DataTable dtZone = News.ZoneBO.GetDetailZoneBO(zoneID.ToString());
            if (dtZone != null)
            {
                DataRow dr = dtZone.Rows[0];
                nameZone = dr[Const.tbZoneNews.url].ToString();
            }
            return string.Format(sFormat, nameZone, title, NewsID);

        }
        public static string BuildLinkZoneNews(string zoneID)
        {
            string kq = "";
            DataTable dtZone = BO.News.ZoneBO.GetDetailZoneBO(zoneID);
            if (dtZone != null && dtZone.Rows.Count > 0)
            {
                DataRow dr = dtZone.Rows[0];
                string url = dr[BO.Const.tbZoneNews.url].ToString();
                string parentID = dr[BO.Const.tbZoneNews.ParentId].ToString();
                //bat dau xet
                if (parentID == "0")
                {
                    //1 cap
                    kq = "/" + url + ".htm";
                }
                else
                {
                    //2 cap
                    DataTable dtZoneP = BO.News.ZoneBO.GetDetailZoneBO(parentID);
                    if (dtZoneP != null && dtZoneP.Rows.Count > 0)
                    {
                        DataRow drP = dtZoneP.Rows[0];
                        string urlP = drP[BO.Const.tbZoneNews.url].ToString();
                        kq = "/" + urlP + "/" + url + ".htm";
                    }
                }
            }
            //
            return kq;
        }
        #endregion
    }
}
